package com.zg.scf.service.impl;


import com.zg.common.core.dao.scf.CompanyApplyDao;
import com.zg.common.core.dao.scf.CompanyDao;
import com.zg.common.core.dao.scf.entity.Company;
import com.zg.common.core.dao.scf.entity.CompanyApply;
import com.zg.common.core.dao.scf.entity.PersonApply;
import com.zg.common.core.service.PageBaseService;
import com.zg.scf.domain.req.company.CompanyAddReq;
import com.zg.scf.enm.CompanyInfoConstants;
import com.zg.scf.service.PersonService;
import com.zg.scf.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/5/22 10:47
 */



@Service
@Slf4j
public class CompanyServiceImpl extends PageBaseService<Company, CompanyDao> implements CompanyService {

    @Resource
    private PersonService personService;

    @Resource
    private CompanyApplyDao companyApplyDao;


    public Company getCompany(Long id){
        return baseDao.getCompany(id);
    }

    public Company getCompanyApply(Long id){
        return companyApplyDao.getCompanyApply(id);
    }

    @Transactional
    @Override
    public void add(CompanyAddReq companyAddReq){
        Long id =super.add(companyAddReq);
        companyAddReq.getAgentAddReq().setCompanyId(id);
        companyAddReq.getAgentAddReq().setPersonType(CompanyInfoConstants.CompanyPersonTypeEnum.AGENT.getValue());
        personService.add(companyAddReq.getAgentAddReq());
        companyAddReq.getContactAddReq().setCompanyId(id);
        companyAddReq.getContactAddReq().setPersonType(CompanyInfoConstants.CompanyPersonTypeEnum.CONTACT.getValue());
        personService.add(companyAddReq.getContactAddReq());
    }

    @Transactional
    public void addApply(CompanyAddReq companyAddReq){
        CompanyApply companyApply = new CompanyApply();
        BeanUtils.copyProperties(companyAddReq,companyApply);
        companyApplyDao.save(companyApply);

        companyAddReq.getAgentAddReq().setCompanyId(companyApply.getId());
        companyAddReq.getAgentAddReq().setPersonType(CompanyInfoConstants.CompanyPersonTypeEnum.AGENT.getValue());
        PersonApply personApply =new PersonApply();
        BeanUtils.copyProperties(companyAddReq.getAgentAddReq(),personApply);
        personService.addApply(personApply);

        companyAddReq.getContactAddReq().setCompanyId(companyApply.getId());
        companyAddReq.getContactAddReq().setPersonType(CompanyInfoConstants.CompanyPersonTypeEnum.CONTACT.getValue());
        personApply =new PersonApply();
        BeanUtils.copyProperties(companyAddReq.getContactAddReq(),personApply);
        personService.addApply(personApply);
    }
}
